package cloud.maque.admin.impl;

import cloud.maque.admin.domain.consts.ConstVar;
import cloud.maque.admin.domain.entity.MqRole;
import cloud.maque.admin.domain.entity.MqRoleGroup;
import cloud.maque.admin.domain.vo.RoleTreeVo;
import cloud.maque.admin.mapper.MqRoleGroupMapper;
import cloud.maque.admin.service.MqRoleGroupService;
import cloud.maque.admin.service.MqRoleService;
import cloud.maque.framework.common.request.PaginateDataResquest;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 4color
 * @description 角色组表
 * @date 2023-03-17
 */
@Service
public class MqRoleGroupServiceImpl extends ServiceImpl<MqRoleGroupMapper, MqRoleGroup> implements MqRoleGroupService {

    @Autowired
    MqRoleService roleService;

    @Override
    public MqRoleGroup insert(MqRoleGroup roleGroup) {
        Assert.hasText(roleGroup.getGroupName(), "组名称不能为空");
        save(roleGroup);
        return roleGroup;
    }

    @Override
    public Boolean delete(String groupId, String tenantId) {

        if (roleService.countByGroupId(groupId) > 0) {
            throw new RuntimeException("还有子角色,不能删除组");
        }

        QueryWrapper<MqRoleGroup> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MqRoleGroup::getTenantId, tenantId);
        wrapper.lambda().eq(MqRoleGroup::getRoleGroupId, groupId);
        return remove(wrapper);
    }

    @Override
    public PageInfo<MqRoleGroup> page(PaginateDataResquest<MqRoleGroup> pageReq) {
        PageHelper.startPage(pageReq.getPageIndex() == null ? 1 : pageReq.getPageIndex(), pageReq.getPageSize() == null ? 10000 : pageReq.getPageSize());
        QueryWrapper<MqRoleGroup> wrapper = new QueryWrapper<>();

        if (pageReq.getData() != null) {
            if (!StrUtil.isEmptyIfStr(pageReq.getData().getGroupName())) {
                wrapper.lambda().like(MqRoleGroup::getGroupName, pageReq.getData().getGroupName());
            }
            if (!StrUtil.isEmptyIfStr(pageReq.getData().getTenantId())) {
                wrapper.lambda().eq(MqRoleGroup::getTenantId, pageReq.getData().getTenantId());
            } else {
                wrapper.lambda().eq(MqRoleGroup::getTenantId, ConstVar.NoObject);
            }
        } else {
            wrapper.lambda().eq(MqRoleGroup::getTenantId, ConstVar.NoObject);
        }
        wrapper.lambda().orderByAsc(MqRoleGroup::getGroupOrder);
        wrapper.lambda().orderByAsc(MqRoleGroup::getRoleGroupId);
        List<MqRoleGroup> lists = this.baseMapper.selectList(wrapper);
        PageInfo<MqRoleGroup> pageInfo = new PageInfo<>(lists);

        return pageInfo;
    }

    @Override
    public List<RoleTreeVo> GroupAndRoleTree(PaginateDataResquest<MqRoleGroup> pageReq) {

        PaginateDataResquest<MqRole> pageRole = new PaginateDataResquest();
        pageRole.setPageIndex(1);
        pageRole.setPageSize(1000);

        if (pageReq.getData() == null) {
            MqRoleGroup gp = new MqRoleGroup();
            gp.setTenantId(ConstVar.NoObject);
            pageReq.setData(gp);
        }

        if (pageReq.getData() != null) {
            MqRole mqRole = new MqRole();
            mqRole.setRoleName(pageReq.getData().getGroupName());
            mqRole.setTenantId(pageReq.getData().getTenantId());
            pageRole.setData(mqRole);
        }

        List<RoleTreeVo> roleTreeVos = new ArrayList<>();
        MqRoleGroup findGroup = new MqRoleGroup();
        findGroup.setTenantId(pageReq.getData().getTenantId());
        pageReq.setData(findGroup);
        PageInfo<MqRoleGroup> pageInfo = page(pageReq);


        PageInfo<MqRole> roles = roleService.page(pageRole);

        pageInfo.getList().forEach(group -> {
            RoleTreeVo treeVo = new RoleTreeVo();
            treeVo.setId(group.getRoleGroupId());
            treeVo.setName(group.getGroupName());
            treeVo.setSort(group.getGroupOrder());
            treeVo.setType("group");

            List<MqRole> roleList = roles.getList().stream().filter(f -> {
                return f.getRoleGroupId().equals(group.getRoleGroupId());
            }).collect(Collectors.toList());


            List<RoleTreeVo> roleTreeVoSubs = new ArrayList<>();
            roleList.forEach(role -> {
                RoleTreeVo treeRoleVo = new RoleTreeVo();
                treeRoleVo.setId(role.getRoleId());
                treeRoleVo.setName(role.getRoleName());
                treeRoleVo.setCode(role.getRoleCode());
                treeRoleVo.setSort(role.getRoleOrder());
                treeRoleVo.setType("role");

                roleTreeVoSubs.add(treeRoleVo);
            });

            treeVo.setChildren(roleTreeVoSubs);
            roleTreeVos.add(treeVo);
        });

        return roleTreeVos;
    }
}
